package com.study.demo.data;

import android.text.TextUtils;

import com.study.demo.bean.ThreadReplyQuote;
import com.study.demo.data.local.ContentLocalDataSource;
import com.study.demo.data.remote.ContentRemoteDataSource;
import com.study.demo.db.ThreadInfo;
import com.study.demo.db.ThreadReply;
import com.study.demo.util.HtmlUtil;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Single;

/**
 * Created by sll on 2016/6/3.
 */
public class ContentRepository implements ContentDataSource {

    private final ContentRemoteDataSource mContentRemoteDataSource;
    private final ContentLocalDataSource mContentLocalDataSource;

    @Inject
    public ContentRepository(ContentRemoteDataSource mContentRemoteDataSource,
                             ContentLocalDataSource mContentLocalDataSource) {
        this.mContentRemoteDataSource = mContentRemoteDataSource;
        this.mContentLocalDataSource = mContentLocalDataSource;
    }

    @Override
    public Single<ThreadInfo> getThreadInfo(String fid, String tid) {
        Single<ThreadInfo> remote = mContentRemoteDataSource.getThreadInfo(fid, tid);
        Single<ThreadInfo> local = mContentLocalDataSource.getThreadInfo(fid, tid);

        Single<ThreadInfo> remoteWithLocalUpdate = remote.doOnSuccess(threadInfo -> {
            if (threadInfo != null && threadInfo.getError() == null) {
                threadInfo.setForumName(threadInfo.getForum().getName());
                mContentLocalDataSource.saveThreadInfo(threadInfo);
            }
        });
        return Single.concat(remoteWithLocalUpdate, local)
                .firstOrError();
    }

    @Override
    public Single<List<ThreadReply>> getReplies(String fid, final String tid, final int page) {
        Single<List<ThreadReply>> remote = mContentRemoteDataSource.getReplies(fid, tid, page);
        Single<List<ThreadReply>> local = mContentLocalDataSource.getReplies(fid, tid, page);

        Single<List<ThreadReply>> remoteWithLocalUpdate =
                remote.doOnSuccess(threadReplies -> {
                    if (threadReplies != null) {
                        for (ThreadReply reply : threadReplies) {
                            reply.setTid(tid);
                            reply.setIsLight(false);
                            reply.setPageIndex(page);
                            saveReply(reply);
                        }
                    }
                });

        return Single.concat(remoteWithLocalUpdate, local)
                .firstOrError();
    }

    @Override
    public Single<List<ThreadReply>> getLightReplies(String fid, final String tid) {
        Single<List<ThreadReply>> remote = mContentRemoteDataSource.getLightReplies(fid, tid);
        Single<List<ThreadReply>> local = mContentLocalDataSource.getLightReplies(fid, tid);

        Single<List<ThreadReply>> remoteWithLocalUpdate =
                remote.doOnSuccess(threadReplies -> {
                    if (threadReplies != null) {
                        for (ThreadReply reply : threadReplies) {
                            reply.setTid(tid);
                            reply.setIsLight(true);
                            saveReply(reply);
                        }
                    }
                });
        return Single.concat(remoteWithLocalUpdate, local)
                .firstOrError();
    }

    private void saveReply(ThreadReply reply) {
        if (!reply.getQuote().isEmpty()) {
            ThreadReplyQuote quote = reply.getQuote().get(0);
            reply.setQuoteHeader(quote.header.get(0));
            if (!TextUtils.isEmpty(quote.togglecontent)) {
                reply.setQuoteToggle(quote.togglecontent);
            }
            reply.setQuoteContent(HtmlUtil.transImgToLocal(quote.content));
        }
        mContentLocalDataSource.saveThreadReply(reply);
    }
}
